/**
 * 依赖：org.springframework.boot:spring-boot-starter-activemq
 * Producer：
 * 1、application.yml文件配置AcitiveMQ相关信息：主要是Connection信息和JMS属性
 * 2、编写配置类，用于创建Queue/Topic
 * 3、注入JmsTemplate/JmsMessagingTemplate对象，通过该象发送消息到Queue/Topic，jmsTemplate.convertAndSend
 *
 * Consumer：
 * 1、application.yml文件配置AcitiveMQ相关信息：主要是Connection信息
 * 2、创建消息处理类，用于接收队列中的消息并进行处理，@JmsListener(destination = "xxx")
 *
 * 启动内嵌ActiveMQ另一种方法：
 * public static void main(String args[]) throws Exception {
 * 		BrokerService broker = new BrokerService();
 * 		// 启用broker的JMX监控功能
 * 		broker.setUseJmx(true);
 * 		// 设置broker名字
 * 		broker.setBrokerName("MyBroker");
 * 		// 是否使用持久化
 * 		broker.setPersistent(false);
 * 		// 添加连接协议，地址
 * 		broker.addConnector("tcp://localhost:61616");
 * 		broker.start();
 * }
 */
package com.example.acitvemqspringbootdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.MessageType;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.jms.ConnectionFactory;

@SpringBootApplication
public class AcitveMqSpringBootDemoApplication {

//    //启动内嵌ActiveMQ须配置
//    @Bean
//    public BrokerFactoryBean activemq() throws Exception {
//        BrokerFactoryBean broker = new BrokerFactoryBean();
//        ClassPathResource res = new ClassPathResource("activemq.xml");
//        broker.setConfig(res);
//        broker.setStart(true);
//        return broker;
//    }
//    //可选：将springboot里面的消息加到jms监听工厂
//    @Bean
//    public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
//                                                    DefaultJmsListenerContainerFactoryConfigurer configurer) {
//        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
//        // This provides all boot's default to this factory, including the message converter
//        configurer.configure(factory, connectionFactory);
//        // You could still override some of Boot's default if necessary.
//        return factory;
//    }
//
//    //可选：转换消息格式, 没有这个会报消息类型转换错误:
//    @Bean // Serialize message content to json using TextMessage
//    public MessageConverter jacksonJmsMessageConverter() {
//        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
//        converter.setTargetType(MessageType.TEXT);
//        converter.setTypeIdPropertyName("_type");
//        return converter;
//    }

    public static void main(String[] args) {
        SpringApplication.run(AcitveMqSpringBootDemoApplication.class, args);
    }

}
